package com.woniuxy.mesboot.controller;


import com.woniuxy.mesboot.entity.MesFactory;
import com.woniuxy.mesboot.entity.MesTeam;
import com.woniuxy.mesboot.entity.MesWorkshop;
import com.woniuxy.mesboot.service.FactoryService;
import com.woniuxy.mesboot.service.TeamService;
import com.woniuxy.mesboot.service.WorkshopService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.dto.MesUnitDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/mesunits")
public class MesUnitController {
    @Resource
    private FactoryService factoryService;

    @Resource
    private WorkshopService workshopService;

    @Resource
    private TeamService teamService;

    @GetMapping("{typeId}")
    public Result queryByType(@PathVariable Integer typeId) {
        switch (typeId) {
            case 0:
                return Result.ok().data("list", factoryService.selectAllName(null));
            case 1:
                return Result.ok().data("list", workshopService.selectAllName(null));
            case 2:
                return Result.ok().data("list", teamService.selectAllName(null));
            default:
                return Result.error().message("没有对应的生产单元");
        }
    }

    @PostMapping("queryInfo")
    public Result queryOneByType(@RequestBody MesUnitDTO dto) {
        switch (dto.getTypeId()) {
            case "0":
                return Result.ok().data("item", factoryService.selectAllName(Long.parseLong(dto.getId().toString())));
            case "1":
                return Result.ok().data("item", workshopService.selectAllName(Long.parseLong(dto.getId().toString())));
            case "2":
                return Result.ok().data("item", teamService.selectAllName(Long.parseLong(dto.getId().toString())));
            default:
                return Result.error().message("没有对应的生产单元");
        }
    }

    @PutMapping
    public Result updateOneByType(@RequestBody MesUnitDTO dto) {
        Integer ret = 0;
        switch (dto.getTypeId()) {
            case "0":
                MesFactory factory = new MesFactory();
                factory.setFtyId(dto.getFtyId());
                factory.setWorkNo(dto.getWorkNo());
                factory.setStatus(dto.getStatus());
                ret = factoryService.updateByPrimaryKeySelective(factory);
                break;
            case "1":
                MesWorkshop workshop = new MesWorkshop();
                workshop.setWorkShopId(dto.getWorkShopId());
                workshop.setWorkNo(dto.getWorkNo());
                workshop.setStatus(dto.getStatus());
                ret = workshopService.updateByPrimaryKeySelective(workshop);
                break;
            case "2":
                MesTeam team = new MesTeam();
                team.setTeamId(dto.getTeamId());
                team.setWorkNo(dto.getWorkNo());
                team.setStatus(dto.getStatus());
                ret = teamService.updateByPrimaryKeySelective(team);
                break;
            default:
                return Result.error().message("没有对应的生产单元");
        }
        if (ret > 0) {
            return Result.ok();
        }
        return Result.error("更新失败");
    }
}
